suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(lubridate))
library(haven)

source("Code/helper_functions.R")

Deaths_municipalities <- read_csv("Data/deaths_2015_to_2021_2022_02_11.csv")

Population_data_extrapolated <- readRDS("Data/Population_data_extrapolated.rds")

Municipality_data <- Deaths_municipalities %>%
  filter(AnnéeDécès >= 2015) %>%
  filter(DépartementDécès != "") %>%
  filter(DépartementDécès != "99") %>% # Outside of France
  filter(CodeCommuneDécès != "000") %>% # We probably do not know which municipality they died in
  mutate(Municipality_code = paste(DépartementDécès, CodeCommuneDécès, sep = "")) %>%
  filter(!is.na(MoisDécès)) %>%
  filter(!is.na(JourDécès)) # Safe option, only removes 102 people


selected_period <- c(2015:2020) %>% setdiff(2019)

Mortality_data <- selected_period %>%
  lapply(function(y) {
    mortality_period_municipality_level(Municipality_data = Municipality_data, start_date = ymd(paste(y, "/07/01", sep = "")), end_date = ymd(paste(y + 1, "/06/30", sep = ""))) %>%
      mutate(year = y)
  }) %>%
  bind_rows()

Mortality_data_joined <- Population_data_extrapolated %>%
  select(Code, Population, year) %>%
  filter(year %in% selected_period) %>%
  rename(Municipality_code = Code) %>%
  left_join(Mortality_data) %>%
  mutate(NbDeath = ifelse(test = is.na(NbDeath), yes = 0, no = NbDeath)) # 0 death when a municipality has no death record


excluded_cities <- Mortality_data_joined %>%
  filter(NbDeath > Population) %>%
  .$Municipality_code %>%
  unique()

selected_cities <- read_csv("Data/council_turnover.csv") %>%
  mutate(Municipality_code = paste(DepartmentCode, MunicipalityCode, sep = "")) %>%
  .$Municipality_code %>%
  (function(x) x[!x %in% c("57031", "57031", "77237", "77237", "90097", "90097", "57031", "77237", "90097", "57031", "77237", "57031", "77237", excluded_cities)]) # These municipalities have more dead than population

full_data <- Mortality_data_joined %>%
  filter(Population > 0) %>%
  filter(Municipality_code %in% selected_cities) %>%
  mutate(covid = ifelse(test = year >= 2020, yes = 1, no = 0)) %>%
  mutate(Municipality_idx = as.numeric(as.factor(Municipality_code))) %>%
  mutate(Municipality_label = as.character(Municipality_idx)) %>%
  mutate(Département = str_sub(string = Municipality_code, start = 1, end = 2)) %>%
  mutate(region = region_from_department(Département))

if (!dir.exists("Data/Saves")) dir.create("Data/Saves")
saveRDS(object = full_data, file = "Data/Saves/full_mortality_data_2015_2021_no_2019_second_semester_2020_first_semester.rds")
